package com.youshang520i.consuldiscoveryprovider;

import com.sun.org.apache.xpath.internal.operations.Bool;
import lombok.Data;
import lombok.ToString;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.swing.*;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @ConfigurationProperties 读取配置文件
 */
@Log4j2
//@RestController
@SpringBootApplication
public class SpringConsulConfig {

    public static void main(String[] args) {
        SpringApplication.run(SpringConsulConfig.class, args);
    }

    //重点： Value 不会起到针对Consul动态加载配置文件的作用
    @Value("${config.env}")
    public String env1;




    // 重点： 会针对Consul配置文件动态加载
    @ConfigurationProperties(prefix = "config")
    @Configuration
    @Data
    @ToString
    public class ConfigProperties {
        private String env;
        private String version;

    }

    @Autowired
    private ConfigProperties configProperties;
    @PostConstruct
    public void init(){
        log.info("env:  " + configProperties);
    }

    // 全局配置
    @ConfigurationProperties(prefix = "jdbc")
    @Configuration
    @Data
    @ToString
    public class JdbcConfigProperties {
        private String url;
        private String username;
        private String password;
    }

    @Configuration
    @Data
    @ToString
    @ConfigurationProperties(prefix = "spring")
    public class CSpring{
        private CloudConfiguration cloud = new CloudConfiguration();
    }

    @Data
    public class CloudConfiguration{
        private ConsulConfiguration consul = new ConsulConfiguration();
    }

    @Data
    public class ConsulConfiguration{
        private ConfigConfiguration config = new ConfigConfiguration();
    }

    @Data
    public class ConfigConfiguration{
        private Boolean enabled;

        private String format;

        private String prefix;

        private String defaultContext;
    }



    @Component
    public class MyRunner implements CommandLineRunner {
        @Autowired
        private ConfigProperties configProperties;
        @Autowired
        private JdbcConfigProperties jdbcConfigProperties;
        @Autowired
        private CSpring cSpring;
        @Override
        public void run(String... args) throws Exception {
            Executors.newSingleThreadExecutor().submit(() -> {
                while (true) {
                    log.info("config -> {}", configProperties);
                    log.info("jdbc-config -> {}", jdbcConfigProperties);
                    log.info("env1 -> {}", env1);
                    TimeUnit.SECONDS.sleep(5);
                }
            });
            System.out.println("cApplication : " + cSpring);
        }
    }
}
